package com.jinmdz.app.dao.dao.provider;


import com.jinmdz.app.dao.base.BaseProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.jdbc.SQL;

/**
 * 服务项目表Provider
 *
 * @author LiCongLu
 * @date 2020-02-12 15:41
 */
public class ServiceItemProvider extends BaseProvider {
    /**
     * 按照业务类型编码获取服务项目
     *
     * @param businessType 业务类型编码
     * @return
     * @author LiCongLu
     * @date 2020-02-12 15:45
     */
    public String listServiceItemByBusinessType(@Param("businessType") String businessType) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" service_item.id,service_type_id,item_code,item_name,item_name_abbr,item_price,item_number");
                builder.append(" ,item_unit,item_describe,as_goods,as_package,as_fix_price,disable_state,sort,remark,service_item.version ");
                SELECT(builder.toString());
                FROM(" service_business_item ");
                JOIN(" service_item on service_item.id = service_business_item.service_item_id  ");
                WHERE(" service_business_item.deleted = 0 and service_item.deleted = 0 and service_item.disable_state = 0");
                WHERE(" service_business_item.business_type = #{businessType} ");
                ORDER_BY(" service_item.sort ");
            }
        }.toString();
    }

    /**
     * 按照业务字典数据编码获取费用项目
     *
     * @param dictCode 字典类型编码
     * @param dataCode 字典数据编码
     * @param purpose  用途
     * @return
     * @author LiCongLu
     * @date 2020-02-12 16:29
     */
    public String getServiceItemByDictCode(@Param("dictCode") String dictCode, @Param("dataCode") Integer dataCode, @Param("purpose") String purpose) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" service_item.id,service_type_id,item_code,item_name,item_name_abbr,item_price,item_number");
                builder.append(" ,item_unit,item_describe,as_goods,as_package,as_fix_price,service_item.sort");
                builder.append(" ,service_item.disable_state,service_item.remark,service_item.version ");
                SELECT(builder.toString());
                FROM(" service_dictionary_item ");
                JOIN(" service_item on service_item.id = service_dictionary_item.service_item_id");
                JOIN(" data_dictionary on data_dictionary.id = service_dictionary_item.data_dictionary_id");
                WHERE(" service_dictionary_item.deleted = 0 and service_item.deleted = 0 and service_item.disable_state = 0 and data_dictionary.deleted = 0");
                WHERE(" data_dictionary.dict_code = #{dictCode} and data_dictionary.data_code = #{dataCode} ");

                // 用途
                if (valid(purpose)) {
                    WHERE(" service_dictionary_item.purpose = #{purpose}");
                }

                ORDER_BY(" service_item.id desc limit 1 ");
            }
        }.toString();
    }

    /**
     * 通过字典编码，获取业务费用规则用途关联字典数据
     *
     * @param dictCode 字典编码
     * @param purpose  用途
     * @return
     * @author LiCongLu
     * @date 2020-03-11 17:23
     */
    public String listServiceDictionaryRuleByDictCode(@Param("dictCode") String dictCode, @Param("purpose") String purpose) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" data_dictionary.id,dict_code,data_code,data_text,data_text_abbr,data_value,data_describe");
                builder.append("  ,as_default,data_dictionary.disable_state,data_dictionary.sort,data_dictionary.remark,data_dictionary.version ");
                builder.append(" , service_item_id,item_price,item_number,item_unit,purpose,rule_code,rule_type,rule_describe,rule_params");
                SELECT(builder.toString());
                FROM(" service_dictionary_item ");
                JOIN(" service_item on service_item.id = service_dictionary_item.service_item_id");
                JOIN(" data_dictionary on data_dictionary.id = service_dictionary_item.data_dictionary_id");
                WHERE(" service_dictionary_item.deleted = 0 and service_item.deleted = 0 and service_item.disable_state = 0 and data_dictionary.deleted = 0");
                WHERE(" data_dictionary.dict_code = #{dictCode} ");
                // 用途
                if (valid(purpose)) {
                    WHERE(" service_dictionary_item.purpose = #{purpose}");
                }
                ORDER_BY(" data_dictionary.sort desc");
            }
        }.toString();
    }



    /**
     * 按照 服务名称 获取服务项目信息
     *
     * @param serviceItemName 服务项目名称
     * @return
     * @author WangShengtao
     * @date 2020-08-24 15:44
     */
    public String getServiceItemByServiceItemName(@Param("serviceItemName") String serviceItemName) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" * from service_item where item_name='接运物品001'");
                SELECT(builder.toString());
                FROM(" service_item ");
                WHERE(" item_name=#{serviceItemName}");
                WHERE(" deleted = 0 limit 1");
                WHERE(" disable_state=0");
            }
        }.toString();
    }
}
